package com.financing.controller.agent;
import java.util.HashMap;
import java.util.Map;

import com.financing.model.Data;
import com.financing.model.invest.Deposit;
import com.financing.model.invest.Repay;
import com.financing.model.invest.Trade;
import com.financing.model.invest.Withdraw;
import com.financing.model.system.AssetsLog;
import com.financing.model.user.Staff;
import com.financing.model.user.User;
import com.financing.service.agent.VisitService;
import com.financing.service.invest.RepayService;
import com.financing.service.invest.TradeService;
import com.financing.service.system.AssetsLogService;
import com.financing.service.user.DepositService;
import com.financing.service.user.StaffService;
import com.financing.service.user.WithdrawService;
import com.financing.util.DateUtil;
import com.financing.util.Json;
import com.financing.util.Money;
import com.financing.util.Str;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;

/**
 *  绩效控制器
 * @since 2017-06-06
 * @author 无处不在的二叔
 */
public class MeritController extends Controller {
	private VisitService visit = new VisitService();
	private StaffService staff = new StaffService();
	private TradeService trade = new TradeService();
	private RepayService repay = new RepayService();
	private DepositService deposit = new DepositService();	
	private WithdrawService withdraw = new WithdrawService();	
	private AssetsLogService assetsLog = new AssetsLogService();	

	/**
	 * 客服绩效数据
	 */
	public void info() { 

		User u = getAttr("user");
		
		// 获取日期截点 
		String start = getPara("start",DateUtil.format("yyyy-MM")+"-01 00:00:00");
		String end = getPara("end",DateUtil.format("yyyy-MM")+"-31 24:00:00");
		Map<Object, Object> data = Data.put("withdraw",0.0,"trade",0.0,"repay",0.0);
		
		// 获取投资数据
		trade.findByAgent(1, 9999, start, end, String.valueOf(u.getId())).getList().forEach(n->{
			data.put("trade", Money.add((double) data.get("trade"), n.getAmount()));
		}); 
		
		// 获取还款数据
		repay.findByAgent(1, 9999, String.valueOf(u.getId()), start, end, "", "", "").getList().forEach(n->{
			data.put("repay", Money.add((double) data.get("repay"), n.getAmount()));
		});
		
		// 获取提现数据
		withdraw.findByAgent(1, 9999, start, end, String.valueOf(u.getId())).getList().forEach(n->{
			data.put("withdraw", Money.add((double) data.get("withdraw"), n.getAmount()));
		});
		
		renderJson(Json.ok("数据获取成功！", Data.put("data", data)));
	}	
	
	/**
	 * 拜访列表
	 */
	public void list() { 
		
		// 获取日期截点
		String start = getPara("start",DateUtil.format("yyyy-MM")+"-01 00:00:00");
		String end = getPara("end",DateUtil.format("yyyy-MM")+"-31 24:00:00");
		
		Map<Integer, Map<Object, Object>> res = new HashMap<Integer, Map<Object, Object>>();
		
		// 获取经纪人
		User temp = getAttr("user");
		boolean isExist = false;
		String agents = visit.getAgents(temp.getId());
		for(String s : visit.getAgents(temp.getId()).split("\\*")){
			if(Str.nb(s)) {
				isExist = true;
				int userId = Integer.valueOf(s);
				res.put(userId, Data.put("staff",staff.findByUserId(userId),"trade",0.0,"repay",0.0,"withdraw",0.0));
			}
		}
		
		if(isExist) {
			
			// 获取投资数据
			trade.list(1, 99999, Data.putStr("applyTimeStart",start,"applyTimeEnd",end,"tyj","0","userId",agents)).getList().forEach(n->{
				User u = User.me.findById(n.getUserId());
				Map<Object, Object> dt = res.get(u.getAgentId());
				dt.put("trade", Money.add((double) dt.get("trade"), n.getAmount()));
			});
			
			// 获取还款数据
			repay.list(1, 99999, Data.putStr("actualRepayTimeStart",start,"actualRepayTimeEnd",end,"userId",agents)).getList().forEach(n->{
				User u = User.me.findById(n.getUserId());
				Map<Object, Object> dt = res.get(u.getAgentId());
				dt.put("repay", Money.add((double) dt.get("repay"), n.getAmount()));
			});
			
			// 获取提现数据
			withdraw.list(1, 99999, Data.putStr("createTime",start,"createTime",end,"userId",agents)).getList().forEach(n->{
				User u = User.me.findById(n.getUserId());
				Map<Object, Object> dt = res.get(u.getAgentId());
				dt.put("withdraw", Money.add((double) dt.get("withdraw"), n.getAmount()));
			});
		}
		
		renderJson(Json.ok("数据获取成功！", Data.put("staff", res)));
		
	}	
	

	/**
	 *  我的客户的投资交易记录
	 */
	public void trade() { 
		User u = getAttr("user");
		int page = getParaToInt("page",1);
		int size = getParaToInt("size",10);
		
		// 获取日期截点
		// String start = getPara("start",DateUtil.format("yyyy-MM")+"-01 00:00:00");
		// String end = getPara("end",DateUtil.format("yyyy-MM")+"-31 24:00:00");

		// 获取经纪人
		String agents = visit.getAgents(u.getId());
		Map<String,String> params = Data.flat(getParaMap());
		params.put("userId", agents);
		
		// 获取投资交易数据
		// Page<Trade> p = trade.findByAgent(page, size, start, end, agents);
		Page<Trade> p = trade.list(page, size, params);
		p.getList().forEach(n->{
			User tu = User.me.findById(n.getUserId());
			n.put("name", tu.getRealName());
			n.put("mobile", tu.getMobile());
			n.put("agent", User.me.findById(tu.getAgentId()).getRealName());
		});

		renderJson(Json.ok("数据获取成功！", Data.put("page", p)));
	}	
	

	/**
	 *  我的客户的还款记录
	 */
	public void repay() { 
		User u = getAttr("user");
		Staff s = staff.findByUserId(u.getId());
		int page = getParaToInt("page",1);
		int size = getParaToInt("size",10);
		
		// 获取经纪人
		String agents = visit.getAgents(u.getId());
		Map<String,String> params = Data.flat(getParaMap());
		params.put("userId", agents);
		
		// 获取还款数据
		// Page<Repay> p = repay.findByAgent(page, size, agents, start, end, mobile, status, name);
		Page<Repay> p = repay.list(page, size, params);
		p.getList().forEach(n->{
			User tu = User.me.findById(n.getUserId());
			n.put("name", tu.getRealName());
			n.put("mobile", tu.getMobile());
			n.put("agent", User.me.findById(tu.getAgentId()).getRealName());
		});
		renderJson(Json.ok("数据获取成功！", Data.put("role",s.getRole(), "page", p)));
	}	
	
	/**
	 * 我的客户的充值记录
	 */
	public void deposit() { 
		User u = getAttr("user");
		int page = getParaToInt("page",1);
		int size = getParaToInt("size",10);

		// 获取经纪人
		String agents = visit.getAgents(u.getId());
		Map<String,String> params = Data.flat(getParaMap());
		params.put("userId", agents);
		
		// 获取还款数据
		Page<Deposit> p = deposit.list(page, size, params);
		p.getList().forEach(n->{
			User tu = User.me.findById(n.getUserId());
			n.put("name", tu.getRealName());
			n.put("mobile", tu.getMobile());
			n.put("agent", User.me.findById(tu.getAgentId()).getRealName());
		});
		renderJson(Json.ok("数据获取成功！", Data.put("page", p)));
	}

	/**
	 * 我的客户的提现记录
	 */
	public void withdraw() { 
		User u = getAttr("user");
		int page = getParaToInt("page",1);
		int size = getParaToInt("size",10);

		// 获取经纪人
		String agents = visit.getAgents(u.getId());
		Map<String,String> params = Data.flat(getParaMap());
		params.put("userId", agents);
		
		// 获取还款数据
		Page<Withdraw> p = withdraw.list(page, size, params);
		p.getList().forEach(n->{
			User tu = User.me.findById(n.getUserId());
			n.put("name", tu.getRealName());
			n.put("mobile", tu.getMobile());
			n.put("agent", User.me.findById(tu.getAgentId()).getRealName());
		});
		renderJson(Json.ok("数据获取成功！", Data.put("page", p)));
	}
	

	/**
	 * 我的客户的操作日志
	 */
	public void assetsLog() { 
		User u = getAttr("user");
		Staff s = staff.findByUserId(u.getId());
		int page = getParaToInt("page",1);
		int size = getParaToInt("size",10);
		HashMap<String,String> params = new HashMap<String,String>();
		if(Str.ne(s.getRole(), "admin")) {
			params.put("agentId", String.valueOf(u.getId()));
		}
		Page<AssetsLog> p = assetsLog.list(page, size, params);
		p.getList().forEach(n->{
			User staff = User.me.findById(n.getAgentId());
			n.put("agent_name", staff!=null ? staff.getRealName() : "无客服");
		});
		renderJson(Json.ok("数据获取成功！", Data.put("role",s.getRole(), "page", p)));
	}
}
 